﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#include "CommandControlDefinition.h"
#include "ICommandManager.h"
#include "ICommand.h"
#include "ICommandAuthorizationFilter.h"
#include "IUiDocumentViewManager.h"

#include "IUiEditModeBehavior.h"
#include "EnableCompileWarning_The_LAST_IncludeInCpp.h"

gcmp::CommandControlDefinition::CommandControlDefinition(const std::wstring& id, const std::wstring& caption, const std::wstring& toolTips, const std::wstring& commandId)
    : m_id(id)
    , m_caption(caption)
    , m_tooltip(toolTips)
    , m_commandId(commandId)
    , m_isChecked(false)
{
}

gcmp::CommandControlDefinition::~CommandControlDefinition()
{

}

bool gcmp::CommandControlDefinition::IsVisible() const
{
    ICommand* pCommand = ICommandManager::Get()->GetCommand(GetCommandId());

    if (!pCommand)
        return true;
    
    return pCommand->IsVisible();
}

bool gcmp::CommandControlDefinition::IsEnabled() const
{
    ICommand* pCommand = ICommandManager::Get()->GetCommand(GetCommandId());

    // 如果没绑定Command则不设置
    if (!pCommand)
        return true;

    const ICommandAuthorizationFilter *pAuthorizedFilter = ICommandAuthorizationFilter::GetCommandAuthorizationFilter();
    if (pAuthorizedFilter != nullptr && !pAuthorizedFilter->IsCommandAutorized(GetCommandId()))
    {
        return false;
    }

    IUiDocument* pUIDoc = IUiDocumentViewManager::Get()->GetCurrentUiDocument();
    if (pUIDoc != nullptr)
    {
        IUiEditModeBehavior* pUIEditMode = UiEditModeUtils::GetCurrentUiEditModeBehavior(pUIDoc);
        if (pUIEditMode != nullptr)
        {
            if (!pUIEditMode->IsCommandEnabled(GetCommandId()))
            {
                return false;
            }
        }
    }
    return pCommand->IsEnabled();
}

bool gcmp::CommandControlDefinition::SetIsChecked(bool isChecked)
{
    m_isChecked = isChecked;
    return true;
}
